package gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;



public class GUIEventEngine extends javax.swing.JFrame implements DebugListener {

    /** Creates new form GUIEventEngine */
    public GUIEventEngine() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {
    	
    	// Set native look and feel
    	WindowUtilities.setNativeLookAndFeel();
        
    	_debugPanel = new javax.swing.JTextArea();
        _processEnginesPanel = new javax.swing.JPanel();
    
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        _debugPanel.setColumns(20);
        _debugPanel.setEditable(false);
        _debugPanel.setRows(5);
           
        _processEnginesPanel.setLayout(new GridLayout(0,3));
        _processEnginesPanel.setPreferredSize(new Dimension(1200, 800));
        //processEnginesPanel.setBorder(BorderFactory.createTitledBorder("Process Engines"));
        
        // Setting scrollable
        JScrollPane debugPanelScroll = new javax.swing.JScrollPane();
        debugPanelScroll.setViewportView(_debugPanel);
        
        // Autoscroll
        debugPanelScroll.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {   
        	public void adjustmentValueChanged(AdjustmentEvent e) {   
        	e.getAdjustable().setValue(e.getAdjustable().getMaximum());   
        	}}); 
        
        JScrollPane enginesScroll = new JScrollPane();
        enginesScroll.setViewportView(_processEnginesPanel);
        
        // Set Frame components
        getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
        getContentPane().add(enginesScroll);
        getContentPane().add(debugPanelScroll);
        this.setTitle("Event Engines Monitor");
        
        pack();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        // TEST
    	GUIEventEngine test = new GUIEventEngine();
        
        GUIProcessEnginePanel piPanel = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel);
        
        GUIProcessEnginePanel piPanel2 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel2);
          
        GUIProcessEnginePanel piPanel3 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel3);
        
        GUIProcessEnginePanel piPanel4 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel4);
        
        GUIProcessEnginePanel piPanel5 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel5);
        
        GUIProcessEnginePanel piPanel6 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel6);
          
        GUIProcessEnginePanel piPanel7 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel7);
        
        GUIProcessEnginePanel piPanel8 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel8);
        
        GUIProcessEnginePanel piPanel9 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel9);
        

        GUIProcessEnginePanel piPanel10 = new GUIProcessEnginePanel();
        test.addProcessInstancePanel(piPanel10);
        
        test.setVisible(true);
        
        piPanel.setProcessInstances("TestyProcess Instances");
        piPanel.setInformationPanel("this Is The Information panel");
        
        piPanel2.setProcessInstances("TestyProcess Instances");
        piPanel2.setInformationPanel("this Is The Information panel");
    }

    // Variables declaration - do not modify
    private JPanel _processEnginesPanel;
    private JTextArea _debugPanel;
    private int _nbEngines = 0;
    
    // End of variables declaration
    
	@Override
	public void printDebug(String text) {
//		_debugPanel.setText(_debugPanel.getText() + "\n" + text);
	}

	public void addProcessInstancePanel(GUIProcessEnginePanel piPanel) {
		// if current row is full
		if ( (_nbEngines % ((GridLayout)_processEnginesPanel.getLayout()).getColumns()) == 0)
		{
			// Increase the number of rows on the gridlayout
			((GridLayout)_processEnginesPanel.getLayout()).setRows
					(((GridLayout)_processEnginesPanel.getLayout()).getRows()+1);			
		}
		
		// Add engine panel
		_nbEngines++;
		_processEnginesPanel.add(piPanel);
		_processEnginesPanel.revalidate();
	}
}
